I ran into memory problems using this function